/**
 * 距离量算
 */
import { handler } from './mapClear'
import { removeInputAction } from './measureArea'
export default () => {
  const viewer = window.viewer
  const Cesium = window.Cesium
  if (!viewer || !Cesium) {
    return
  }
  removeInputAction()
  if (handler.measureArea) {
    handler.measureArea.deactivate()
  }
  if (handler.measureHeight) {
    handler.measureHeight.deactivate()
  }
  if (handler.measureDistance) {
    handler.measureDistance.clear()
    handler.measureDistance.activate()
    return handler
  }
  handler.measureDistance = new Cesium.MeasureHandler(
    viewer,
    Cesium.MeasureMode.Distance,
    0
  )
  handler.measureDistance.measureEvt.addEventListener(distance)
  // 激活事件，监听当前事件以获取处理器的状态
  handler.measureDistance.activeEvt.addEventListener(activeFun)
  handler.measureDistance.activate()
  function distance(result) {
    const dis = Number(result.distance)
    const distance =
      dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm'
    handler.measureDistance.disLabel.text = '距离:' + distance
  }
  function activeFun(isActive) {
    if (isActive === true) {
      viewer.enableCursorStyle = false
      viewer._element.style.cursor = 'default'
    } else {
      viewer.enableCursorStyle = true
    }
  }
  return handler
}
